package com.shqcjd.yunweiyanoperator.ui.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.gyf.immersionbar.ImmersionBar;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.api.ApiService;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxObserver;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.yunweiyanoperator.model.ResponseData;
import com.shqcjd.yunweiyanoperator.model.pojo.GoRegister;
import com.shqcjd.yunweiyanoperator.ui.activity.MessageListActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.MsgActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.remote.DeviceConnectSettingActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.remote.DeviceListActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.staff.BusinessExpertActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.staff.ContactsActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.staff.PersonalExpertActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.staff.SendVideoActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.staff.VideotapeListActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BaseFragment;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.PrefUtils;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 远程指导
 */
public class RemoteGuideFragment extends BaseFragment {

    @BindView(R.id.iv_top_bg)
    ImageView ivTopBg;
    @BindView(R.id.fl_business_expert)
    FrameLayout flBusinessExpert;
    @BindView(R.id.fl_expert)
    FrameLayout flExpert;
    @BindView(R.id.ll_send_guide)
    LinearLayout llSendGuide;
    @BindView(R.id.ll_send_video)
    LinearLayout llSendVideo;
    @BindView(R.id.iv_msg)
    ImageView ivMsg;
    @BindView(R.id.iv_red_dot)
    ImageView ivRedDot;
    private int userType;

    public static RemoteGuideFragment newInstance() {
        return new RemoteGuideFragment();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_remote_guide;
    }

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .statusBarColor(R.color.topbgcolor)
                .statusBarDarkFont(true, 0.2f)
                .navigationBarDarkIcon(true)
                .init();
    }

    @Override
    public void initView(View rootView) {
        userType = PrefUtils.getInt(getContext(), PrefUtils.USER_TYPE, -1);
    }

    @OnClick({R.id.fl_business_expert, R.id.fl_expert, R.id.ll_send_guide, R.id.ll_send_video, R.id.iv_msg})
    public void onViewClicked(View view) {
        if (userType == 3) {//游客
            showLoginDialog();
            return;
        }
        Intent intent;
        switch (view.getId()) {
            case R.id.fl_business_expert://查找企业专家
                startActivity(new Intent(getActivity(), BusinessExpertActivity.class));
                break;
            case R.id.fl_expert://查找专家
                startActivity(new Intent(getContext(), PersonalExpertActivity.class));
                break;
            case R.id.ll_send_guide://发起指导
                intent = new Intent(getContext(), ContactsActivity.class);
                intent.putExtra("type", 0);
                startActivity(intent);
                break;
            case R.id.ll_send_video://发送现场录像
                intent = new Intent(getContext(), SendVideoActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_msg://消息
                startActivity(new Intent(getContext(), MessageListActivity.class));

                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (userType != 3) {//非游客
            getMessageNumHttp();
        }

    }

    private void showLoginDialog() {
        final MaterialDialog dialog = new MaterialDialog.Builder(getContext())
                .customView(R.layout.custome_login_layout, false)
                .show();
        dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        View customeView = dialog.getCustomView();
        TextView tv_confirm = customeView.findViewById(R.id.tv_confirm);
        TextView tv_cancel = customeView.findViewById(R.id.tv_cancel);
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new GoRegister());
                dialog.dismiss();
            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    /**
     * 每次切到这个fragment的时候  都需要请求一次获取未读消息的接口
     */
    public void getMessageNumHttp() {
        ApiService.getMessageNum()
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<Integer>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<Integer> responseData) {
                        if (responseData.retCode == 10000) {
                            int num = responseData.data;
                            if(num>0){
                                ivRedDot.setVisibility(View.VISIBLE);
                            }else{
                                ivRedDot.setVisibility(View.INVISIBLE);
                            }
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

}
